/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.isip.transformation;

import com.azon.ip.utils.ImageReader;

/**
 *
 * @author Ahmad Fauzan
 */
public class LinearTransformation implements Transformation{
    
    Transformation.Type type = Transformation.Type.RGB;
    
    public LinearTransformation() {
        
    }
    
    public LinearTransformation(Transformation.Type type) {
        this.type = type;
    }
    
    public int[][] doRGBTransform(int[][] matrix) {
        
        int[][] new_matrix = new int[matrix.length][matrix[0].length];
         for(int x =0; x < matrix.length; x++) {
            for(int y=0; y < matrix[x].length; y++) {
                new_matrix[x][y] = 0;
            }
        }
        
         for(int i=0; i < 3; i++) {
            int sample_matrix[][] = new int[matrix.length][matrix[0].length];
            
            for(int x =0; x < matrix.length; x++) {
                for(int y=0; y < matrix[x].length; y++) {
                    sample_matrix[x][y] = (matrix[x][y] >> (8 * (2 - i))) & 0xff;
                }
            }
            
            int[] histogram = ImageReader.ReadHistogramInGrayScale(sample_matrix);
            float a,b;
            int min = 0;
            int max = histogram.length-1;

            while(histogram[min]==0) {
                min++;
            }

            while(histogram[max]==0) {
                max--;
            }

            a = 255/(float)(max-min);
            b = - a*min;
            System.out.println(min + "-" + max);
            System.out.println(a + "," + b);
            for(int k=0; k<sample_matrix.length; k++) {
                for(int j=0; j < sample_matrix[k].length; j++) {
                    new_matrix[k][j] = (new_matrix[k][j] << 8) + (int)(a*sample_matrix[k][j] + b);
                }
            }
        }
        
        return new_matrix;
    }
    
    public int[][] doGrayScaleTransform(int[][] matrix) {
        int[][] new_matrix = new int[matrix.length][matrix[0].length];
        int[] histogram = ImageReader.ReadHistogramInGrayScale(matrix);
        float a,b;
        int min = 0;
        int max = histogram.length-1;
        
        while(histogram[min]==0) {
            min++;
        }
        
        while(histogram[max]==0) {
            max--;
        }
        
        a = 255/(float)(max-min);
        b = - a*min;
        System.out.println(min + "-" + max);
        System.out.println(a + "," + b);
        for(int i=0; i<matrix.length; i++) {
            for(int j=0; j < matrix[i].length; j++) {
                new_matrix[i][j] = (int)(a*matrix[i][j] + b);
            }
        }
        
        return new_matrix;
    }

    @Override
    public int[][] doTransform(int[][] matrix) {
        if(type == Type.RGB) {
            System.out.println("RGB");
            return doRGBTransform(matrix);
        } else {
            return doGrayScaleTransform(matrix);
        }
    }
    
    
    
}
